package fr.miage.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import fr.miage.connexion.ConnexionBD;
import fr.miage.model.Event;

public class EventDAO {
	
	private Logger logger = Logger.getRootLogger();

	public Event getById(String id) {		
		ResultSet res = null;
		String query = "select * from event WHERE idEvent="+id;
		Statement st = null;
		Event event = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeQuery(query);
			if(res.next()){
				event = getEvent(res);
			}else{
				return null;
			}
		} catch (SQLException ex) {
			logger.error(ex);
		} finally {
			try {
				if (res != null) {
					res.close();
				}
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		
		return event;
	}

	public List<Event> getAllByUser(String idUser) {
		ResultSet res = null;
		String query = "select * from (invitation i join event e on  i.idEvent=e.idEvent) WHERE i.idUser="+idUser;
		Statement st = null;
		List<Event> listeEvent = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeQuery(query);
			listeEvent = new ArrayList<Event>();
			while(res.next()){
				Event event = getEvent(res);
				listeEvent.add(event);
			}
		} catch (SQLException ex) {
			logger.error(ex);
		} finally {
			try {
				if (res != null) {
					res.close();
				}
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
			}
		}
		
		return listeEvent;
	}
	
	private Event getEvent(ResultSet res){
		Event event = new Event();
		try {
			event.setIdEvent(res.getLong("idEvent"));
			event.setIntitule(res.getString("entitled"));
			event.setDate(res.getString("date"));
			event.setTypeEvent(res.getString("eventType"));
			event.setIdAdresse(res.getLong("idAdress"));
			event.setIdCreateur(res.getLong("idCreator"));
		} catch (SQLException e) {
			logger.error(e);
		}
		return event;
	}

	public int addEvent(Event event) {
		int res = 0;
		String query = "insert into event(entitled, date, eventType, idAdress, idCreator) values ('"+event.getIntitule()+"', '"+event.getDate()+"', '"+event.getTypeEvent()+"', "+event.getIdAdresse()+", "+event.getIdCreateur()+")";
		Statement st = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeUpdate(query);
			return res;
		} catch(MySQLIntegrityConstraintViolationException e){
			logger.error(e);
			e.printStackTrace();
			return -1;
		} catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
			return res;
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

	public int delete(String id) {
		int res = 0;
		String query = "delete from event where idEvent="+id;
		Statement st = null;
		try {
			st = ConnexionBD.getInstance().getConn().createStatement();
			res = st.executeUpdate(query);
			return res;
		} catch (SQLException ex) {
			logger.error(ex);
			ex.printStackTrace();
			return res;
		} finally {
			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

}
